home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11902 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  75 lines

  1. Path: GEOMAT.math.uni-hamburg.de!not-for-mail
  2. From: ms5a018@GEOMAT.math.uni-hamburg.de (Christian Duehl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: scanf/gets interaction ?
  5. Date: 27 Mar 1996 11:10:58 GMT
  6. Organization: University of Hamburg -- Germany
  7. Message-ID: <4jb7o2$5n1@rzsun02.rrz.uni-hamburg.de>
  8. References: <4j6joa$6ve@muller.loria.fr>
  9. NNTP-Posting-Host: rzaixsrv21-fi.rrz.uni-hamburg.de
  10. X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
  11.  
  12. Denis B. Roegel (roegel@loria.fr) wrote:
  13. : I have the following program:
  14. : #include <stdio.h>
  15. : main(){
  16. : int n;
  17. : char num[10];
  18. : char*r;
  19. : fflush(stdin);
  20. : printf("n: ");scanf("%i",&n);
  21. : printf("Numero ? ");r = gets(num);
  22. : printf("Bien recu!\n");
  23. : }
  24. : which I compile with gcc on SunOS. The program asks for a first number n which
  25. : I enter. But I never get a chance of entering a second one. Why is this so ?
  26. : Thanks in advance,
  27. : Denis
  28.  
  29. Perhaps there is a loop missing :-)
  30.  
  31. (In K&R I read that fflush with an input stream has a not defined effect...)
  32.  
  33. I think you could want your program so:
  34.  
  35. #include <stdio.h>
  36.  
  37. int main(void)
  38. {
  39.   int n;
  40.   char num[10];  /* What happens if the input is longer than 9 chars???!!! */
  41.   char *r;
  42.  
  43.   /* fflush(stdin); */    /* undefined effect ... (K&R) */
  44.   printf("n: ");
  45.   scanf("%i",&n);
  46.   r = gets(num);
  47.   while (n>0) {
  48.     printf("Numero ? ");
  49.     r = gets(num);
  50.     printf("Bien recu!\n");
  51.     n--;
  52.   }
  53.  
  54.   return 0;
  55. } /* main */
  56.  
  57. ------------------------
  58.  
  59. the function main has to be from type int, ...(void)... is better then ...()...
  60. IMHO.
  61.  
  62. I hope that helps...
  63. Christian.
  64. -- 
  65.  
  66. -------------------------------------------------------------------------
  67. Christian Duehl                  e-mail: duehl@geomat.math.uni-hamburg.de
  68.                                                (duehl@tu-harburg.d400.de)
  69. WWW-Homepage:     http://www.math.uni-hamburg.de/home/duehl/homepage.html
  70.